﻿using System;
using System.Collections.Generic;
using System.Data;
using Dao.Base;

namespace ConsoleApplicationTool
{
    public class DataTransferTool
    {
        static string datasource = @"E:\SanGuoZhi\Db\194_QXGJ.db";
        public static void CreateDB()
        {
            //创建一个数据库文件

            System.Data.SQLite.SQLiteConnection.CreateFile(datasource);
        }
        public static void UpdateOfficer()
        {
            OledbUtil db = new OledbUtil(@"E:\MyProject\SanGuoZhi\SanGuo\SanGuo\bin\x86\Debug\GameData\Scenario\194QXGJ.mdb");
            DataTable tb = db.GetDataTable("Select * From Person");
            SqliteHelper helper = new SqliteHelper();
            List<int> list = new List<int>();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                DataRow dr = tb.Rows[i];
                int id = Convert.ToInt16(dr["ID"]);
                int LoginBuildingId = Convert.ToInt16(dr["AvailableLocation"]);
                if(LoginBuildingId!=999)
                helper.ExecuteNonQuery("update Officer set LoginBuildingId=" + LoginBuildingId + " where Id=" + id);
            }

        }
        public static void Updatebuilding()
        {
            OledbUtil db = new OledbUtil(@"E:\SanGuoZhi\SanGuo\SanGuo\bin\x86\Debug\GameData\Scenario\194QXGJ.mdb");
            DataTable tb = db.GetDataTable("Select * From Architecture");
            SqliteHelper helper = new SqliteHelper();
            List<int> list = new List<int>();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                DataRow dr = tb.Rows[i];
                int id = Convert.ToInt16(dr["ID"]);
                int Population = Convert.ToInt32(dr["Population"]);

                helper.ExecuteNonQuery("update Building set SoldierPopulation="+(int)(Population*0.2)+" where Id=" + id);
            }
         
        }
        public static void InserToOfficer()
        {
            OledbUtil db = new OledbUtil(@"E:\SanGuoZhi\SanGuo\SanGuo\bin\x86\Debug\GameData\Scenario\194QXGJ.mdb");
            DataTable tb = db.GetDataTable("Select * From Person");

            OledbUtil db2 = new OledbUtil(@"E:\SanGuoZhi\SanGuo\SanGuo\bin\x86\Debug\GameData\Scenario\194QXGJ.mdb");
            DataTable tb2 = db2.GetDataTable("Select * From Architecture");
            Dictionary<int, int> dic = new Dictionary<int, int>();
            for (int i = 0; i < tb2.Rows.Count; i++)
            {
                DataRow dr = tb2.Rows[i];
                int id = Convert.ToInt16(dr["ID"]);
                string ars = dr["Persons"].ToString();
                string[] tempArea2 = ars.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var s in tempArea2)
                {
                    dic.Add(Convert.ToInt32(s), id);
                }
            }
            SqliteHelper helper = new SqliteHelper();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                DataRow dr = tb.Rows[i];
                int id = Convert.ToInt16(dr["ID"]);
                string FirstName = dr["SurName"].ToString();
                string Name = dr["GivenName"].ToString();
                string OtherName = dr["CalledName"].ToString();
                bool Sex = Convert.ToBoolean(dr["Sex"]);
                string PicNumber = dr["Pic"].ToString();
                bool IsLive = Convert.ToBoolean(dr["Alive"]);
                int YearAvailable = Convert.ToInt16(dr["YearAvailable"]);
                int YearBorn = Convert.ToInt16(dr["YearBorn"]);
                int YearDead = Convert.ToInt16(dr["YearDead"]);
                int Strength = Convert.ToInt16(dr["Strength"]);
                int Commander = Convert.ToInt16(dr["Command"]);
                int Intelligence = Convert.ToInt16(dr["Intelligence"]);
                int Politics = Convert.ToInt16(dr["Politics"]);
                int Glamour = Convert.ToInt16(dr["Glamour"]);
                int Reputation = Convert.ToInt32(dr["Reputation"]);
                string BuildingID = "NULL";
                if (dic.ContainsKey(id))
                    BuildingID = dic[id].ToString();
                helper.ExecuteNonQuery("insert into Officer values(" + id + ",\'" + FirstName + "\',\'" + Name + "\',\'" + OtherName + "\'," + (Sex ? 0 : 1) + ",\'" + PicNumber
                    + "\'," + (IsLive ? 1 : 0) + "," + YearAvailable + "," + YearBorn + "," + YearDead + "," + Strength + "," + Commander + "," + Intelligence
                    + "," + Politics + "," + Glamour + "," + Reputation + "," + BuildingID + ")");
            }
        }
        public static void InsertToForce()
        {
            OledbUtil db = new OledbUtil(@"E:\SanGuoZhi\SanGuo\SanGuo\bin\x86\Debug\GameData\Scenario\194QXGJ.mdb");
            DataTable tb = db.GetDataTable("Select * From Faction");
            OledbUtil db2 = new OledbUtil(@"E:\SanGuoZhi\SanGuo\SanGuo\bin\x86\Debug\GameData\Common\CommonData.mdb");
            DataTable tb2 = db2.GetDataTable("Select * From Color");
            Dictionary<int, string> dic = new Dictionary<int, string>();
            for (int i = 0; i < tb2.Rows.Count; i++)
            {
                DataRow dr = tb2.Rows[i];
                int id = Convert.ToInt16(dr["ID"]);
                string code = dr["Code"].ToString();
                dic.Add(id, code);

            }
            SqliteHelper helper = new SqliteHelper();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                DataRow dr = tb.Rows[i];
                int id = Convert.ToInt16(dr["ID"]);
                string Name = dr["FName"].ToString();
                bool Passed = Convert.ToBoolean(dr["Passed"]);
                int LeaderID = Convert.ToInt16(dr["LeaderID"]);
                int ColorIndex = Convert.ToInt16(dr["ColorIndex"]);
                string Colorcode = "NULL";
                if (dic.ContainsKey(ColorIndex))
                    Colorcode = dic[ColorIndex];
                int CapitalID = Convert.ToInt16(dr["CapitalID"]);
                int Reputation = Convert.ToInt32(dr["Reputation"]);
                helper.ExecuteNonQuery("insert into Force values(" + id + ",\'" + Name + "\'," + (Passed ? 1 : 0) + "," +
                  LeaderID + ",\'" + Colorcode + "\'," + CapitalID + "," + Reputation + ")");
            }
        }
        public static void InsertToBuilding()
        {

            OledbUtil db = new OledbUtil(@"\GameData\Scenario\194QXGJ.mdb");
            DataTable tb = db.GetDataTable("Select * From Architecture");
            DataTable tb2 = db.GetDataTable("Select ID,Architectures From Faction");
            Dictionary<int, int> dic = new Dictionary<int, int>();
            for (int i = 0; i < tb2.Rows.Count; i++)
            {
                DataRow dr = tb2.Rows[i];
                int id = Convert.ToInt16(dr["ID"]);
                string ars = dr["Architectures"].ToString();
                string[] tempArea2 = ars.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var s in tempArea2)
                {
                    dic.Add(Convert.ToInt32(s), id);
                }
            }
            SqliteHelper helper = new SqliteHelper();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                DataRow dr = tb.Rows[i];
                int id = Convert.ToInt16(dr["ID"]);
                int? forceid = null;
                if (dic.ContainsKey(id))
                    forceid = dic[id];
                string numberName = dr["CaptionID"].ToString();
                string name = dr["Name"].ToString();
                bool hasEmper = Convert.ToBoolean(dr["Emperor"].ToString());
                string cityTypeId = dr["Kind"].ToString();
                string position = dr["Area"].ToString();
                string forceid2 = (forceid == null ? "NULL" : forceid.ToString());
                helper.ExecuteNonQuery("insert into Building values(" + id + "," + forceid2 + ",\'" + cityTypeId + "\',\'" +
                    numberName + "\',\'" + name + "\'," + (hasEmper ? 1 : 0) + ",\'" + position + "\')");
            }

        }
        public static void InsertToTreasure()
        {

            OledbUtil db = new OledbUtil(@"F:\WorldOfTheThreeKingdomsV32a_chs\GameData\Scenario\194QXGJ.mdb");
            DataTable tb = db.GetDataTable("Select * From Treasure");
           
            SqliteHelper helper = new SqliteHelper();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                DataRow dr = tb.Rows[i];
                int id = Convert.ToInt16(dr["ID"]);
                string Name = dr["Name"].ToString();
                string Pic = dr["Pic"].ToString();
                bool Available = Convert.ToBoolean(dr["Available"].ToString());
                int TreasureGroup = Convert.ToInt32(dr["TreasureGroup"].ToString());
                short AppearYear = Convert.ToInt16(dr["AppearYear"].ToString());
                string Description = dr["Description"].ToString();
                int BelongedPerson = Convert.ToInt32(dr["BelongedPerson"].ToString());
                int BuildingId = Convert.ToInt32(dr["HidePlace"].ToString());
                int isMutilate = BelongedPerson == -1 ? 1 : 0;
                helper.ExecuteNonQuery("insert into Treasure values(" + id + ",\'" + Name + "\',1,\'" +
                    Pic + "\'," + (Available ? 1 : 0) + ","+isMutilate+"," + TreasureGroup + ","+AppearYear+",\'" + Description + "\',0,0,0,0)");
                if(BelongedPerson!=-1)
                helper.ExecuteNonQuery("insert into OfficerHasTreasure values(" + id + "," + BelongedPerson + ")");
                if(BuildingId!=-1)
                helper.ExecuteNonQuery("insert into BuildingHasTreasure values("+BuildingId+"," + id + ")");
            }

        }
    }
}
